package com.youyi.yyviewsdklibrary.View;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lkx.liandianqinew.R;
import com.youyi.yyviewsdklibrary.Dialog.util.ToastUtil;

public class MyEditView extends LinearLayout {
    private LinearLayout mBg;
    private ImageView mEditClear;
    private EditText mEditText;
    private OnTextChangeListener mOnTextChangeListener;

    public interface OnTextChangeListener {
        void change(String str);

        void onclick(String str);
    }

    public MyEditView(Context context) {
        super(context);
    }

    public void setOnTextChangeListener(OnTextChangeListener onTextChangeListener) {
        this.mOnTextChangeListener = onTextChangeListener;
    }

    public MyEditView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        ToastUtil.err("MyEditView");
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.MyEditView);
        String string = obtainStyledAttributes.getString(R.styleable.MyEditView_yyEditHint);
        boolean z = obtainStyledAttributes.getBoolean(R.styleable.MyEditView_yyEditTextCenter, false);
        int resourceId = obtainStyledAttributes.getResourceId(R.styleable.MyEditView_yyEditSearchBg, R.drawable._yy_whitebg_20dp);
        int i = obtainStyledAttributes.getInt(R.styleable.MyEditView_yyEditType, 0);
        View inflate = LayoutInflater.from(context).inflate(R.layout._yy_edit_view, (ViewGroup) this, true);
        LinearLayout linearLayout = (LinearLayout) inflate.findViewById(R.id.view_yy_layout);
        this.mBg = linearLayout;
        linearLayout.setBackgroundResource(resourceId);
        this.mEditText = (EditText) inflate.findViewById(R.id.bt_yy_edit);
        this.mEditClear = (ImageView) inflate.findViewById(R.id.bt_yy_clear);
        if (!TextUtils.isEmpty(string)) {
            this.mEditText.setHint(string);
        }
        if (i == 0) {
            this.mEditText.setInputType(1);
        } else if (i == 1) {
            this.mEditText.setInputType(8194);
        } else if (i == 2) {
            this.mEditText.setInputType(128);
        } else if (i == 3) {
            this.mEditText.setInputType(3);
        }
        this.mEditText.setHorizontallyScrolling(false);
        this.mEditText.setSingleLine(false);
        this.mEditText.setGravity(z ? 17 : 16);
        this.mEditClear.setOnClickListener(new OnClickListener() {
            /* class com.youyi.yyviewsdklibrary.View.MyEditView.AnonymousClass1 */

            public void onClick(View view) {
                MyEditView.this.mEditText.setText("");
            }
        });
        this.mEditText.addTextChangedListener(new TextWatcher() {
            /* class com.youyi.yyviewsdklibrary.View.MyEditView.AnonymousClass2 */

            public void afterTextChanged(Editable editable) {
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                String charSequence2 = charSequence.toString();
                MyEditView.this.mEditClear.setVisibility(TextUtils.isEmpty(charSequence2) ? View.GONE : View.VISIBLE);
                if (MyEditView.this.mOnTextChangeListener != null) {
                    MyEditView.this.mOnTextChangeListener.change(charSequence2);
                }
            }
        });
    }

    public void setText(String str) {
        if (str != null) {
            this.mEditText.setText(str);
            this.mEditText.setSelection(str.length());
        }
    }

    public void setInputType(int i) {
        try {
            this.mEditText.setInputType(i);
        } catch (Exception unused) {
        }
    }

    public void setHint(String str) {
        try {
            this.mEditText.setHint(str);
        } catch (Exception unused) {
        }
    }

    public void openKeyBoard(boolean z) {
        if (z) {
            openKeybord(getContext(), this.mEditText);
        } else {
            closeKeybord(getContext(), this.mEditText);
        }
    }

    public String getText() {
        try {
            return this.mEditText.getText().toString().trim();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void openKeybord(Context context, EditText editText) {
        try {
            editText.setFocusable(true);
            editText.requestFocus();
            InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(editText, 2);
            inputMethodManager.toggleSoftInput(2, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeKeybord(final Context context, final EditText editText) {
        try {
            editText.post(new Runnable() {
                /* class com.youyi.yyviewsdklibrary.View.MyEditView.AnonymousClass3 */

                public void run() {
                    try {
                        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (editText.getWindowToken() != null) {
                            inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
